| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 105 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** | ||
| 22 | 	$.fn.symphonyTimeAgo = function(options) { | ||
| 23 | var objects = this, | ||
| 24 | 			settings = { | ||
| 25 | items: 'time', | ||
| 26 | timestamp: 'utc', | ||
| 27 | max: 0 | ||
| 28 | }; | ||
| 29 | |||
| 30 | $.extend(settings, options); | ||
| 31 | |||
| 32 | /*-----------------------------------------------------------------------*/ | ||
| 33 | |||
| 34 | 		Symphony.Language.add({ | ||
| 35 | 'just now': false, | ||
| 36 | 'a minute ago': false, | ||
| 37 | 			'{$minutes} minutes ago': false, | ||
| 38 | 'about 1 hour ago': false, | ||
| 39 | 			'about {$hours} hours ago': false | ||
| 40 | }); | ||
| 41 | |||
| 42 | /*------------------------------------------------------------------------- | ||
| 43 | Functions | ||
| 44 | -------------------------------------------------------------------------*/ | ||
| 45 | |||
| 46 | 		function parse(item) { | ||
| 47 | 			var timestamp = item.data('timestamp'), | ||
| 48 | datetime; | ||
| 49 | |||
| 50 | // Fetch stored timestamp | ||
| 51 | 			if($.isNumeric(timestamp)) { | ||
| 52 | return timestamp; | ||
| 53 | } | ||
| 54 | |||
| 55 | // Parse date | ||
| 56 | 			else { | ||
|  | |||
| 57 | datetime = item.attr(settings.timestamp); | ||
| 58 | |||
| 59 | // Defined date and time | ||
| 60 | 				if(datetime) { | ||
| 61 | // Datetime will be in seconds since Epoch, JS requires | ||
| 62 | // millseconds, so multiply by 1000. | ||
| 63 | timestamp = new Date(datetime * 1000); | ||
| 64 | } | ||
| 65 | |||
| 66 | // Undefined date and time | ||
| 67 | 				else { | ||
| 68 | timestamp = new Date().getTime(); | ||
| 69 | } | ||
| 70 | |||
| 71 | // Store and return timestamp | ||
| 72 | 				item.data('timestamp', timestamp); | ||
| 73 | return timestamp; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 		function say(from, to) { | ||
| 78 | |||
| 79 | // Calculate time difference | ||
| 80 | var distance = to - from, | ||
| 81 | |||
| 82 | // Convert time to minutes | ||
| 83 | time = Math.floor(distance / 60000); | ||
| 84 | |||
| 85 | // Return relative date based on passed time | ||
| 86 | 			if(time < 1) { | ||
| 87 | 				return Symphony.Language.get('just now'); | ||
| 88 | } | ||
| 89 | 			if(time < 2) { | ||
| 90 | 				return Symphony.Language.get('a minute ago'); | ||
| 91 | } | ||
| 92 | 			if(time < 45) { | ||
| 93 | 				return Symphony.Language.get('{$minutes} minutes ago', { | ||
| 94 | 'minutes': time | ||
| 95 | }); | ||
| 96 | } | ||
| 97 | 			if(time < 90) { | ||
| 98 | 				return Symphony.Language.get('about 1 hour ago'); | ||
| 99 | } | ||
| 100 | 			else if (!settings.max || time < settings.max) { | ||
| 101 | 				return Symphony.Language.get('about {$hours} hours ago', { | ||
| 102 | 'hours': Math.floor(time / 60) | ||
| 103 | }); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | /*------------------------------------------------------------------------- | ||
| 108 | Initialisation | ||
| 109 | -------------------------------------------------------------------------*/ | ||
| 110 | |||
| 111 | 		objects.find(settings.items).each(function timeago() { | ||
| 112 | var item = $(this), | ||
| 113 | from = parse(item), | ||
| 114 | to = new Date(), | ||
| 115 | rel = say(from, to); | ||
| 116 | |||
| 117 | // Set relative time | ||
| 118 | 			if (rel) { | ||
| 119 | item.text(rel); | ||
| 120 | } | ||
| 121 | }); | ||
| 122 | |||
| 123 | /*-----------------------------------------------------------------------*/ | ||
| 124 | |||
| 125 | return objects; | ||
| 126 | }; | ||
| 127 | |||
| 129 |